<template>
  <a-drawer
    title="修改住宅"
    :maskClosable="false"
    width="750"
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="houseEditVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;"
  >
    <a-form :form="form">
      <a-form-item label="住宅编码" v-bind="formItemLayout">
        <a-input
          v-decorator="[
            'houseCode',
            {
              rules: [
                { required: true, message: '区域名称不能为空' },
                { max: 20, message: '长度不能超过20个字符' }
              ]
            }
          ]"
        />
      </a-form-item>
      <a-form-item label="住宅地址" v-bind="formItemLayout">
        <a-input-number v-decorator="['houseAdr']" style="width: 100%" />
      </a-form-item>

      <a-form-item label="所属片区" v-bind="formItemLayout">
        <a-input-search
          v-decorator="[
            'areaName',
            { rules: [{ max: 20, message: '长度不能超过20个字符' }] }
          ]"
          enter-button="搜索"
          @search="showAreaSearch"
        >
        </a-input-search>
      </a-form-item>
      <!-- <a-form-item label='上级区域'
                   style="margin-bottom: 2rem"
                   v-bind="formItemLayout">
        <a-tree
          v-model="checkedKeys"
          ref="areaTree"
          :checkable="true"
          :checkStrictly="true"
          @check="handleCheck"
          @expand="handleExpand"
          :expandedKeys="expandedKeys"
          :defaultCheckedKeys="defaultCheckedKeys"
          :treeData="areaTreeData">
        </a-tree>
      </a-form-item> -->
    </a-form>
    <div class="drawer-bootom-button">
      <a-dropdown
        style="float: left"
        :trigger="['click']"
        placement="topCenter"
      >
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="expandAll">展开所有</a-menu-item>
          <a-menu-item key="2" @click="closeAll">合并所有</a-menu-item>
        </a-menu>
        <a-button> 树操作 <a-icon type="up" /> </a-button>
      </a-dropdown>
      <a-popconfirm
        title="确定放弃编辑？"
        @confirm="onClose"
        okText="确定"
        cancelText="取消"
      >
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="loading"
        >提交</a-button
      >
    </div>
    <!-- 区域查询 -->
    <area-search
      @close="handleAreaSearchClose"
      @success="handleAreaSearchSuccess"
      :areaVisible="areaSearch.areaVisible"
    >
    </area-search>
  </a-drawer>
</template>
<script>
import AreaSearch from './../shop/AreaSearch'
const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}
export default {
  name: 'houseEdit',
  props: {
    houseEditVisiable: {
      default: false
    }
  },
  components: { AreaSearch },
  data () {
    return {
      loading: false,
      formItemLayout,
      form: this.$form.createForm(this),
      tHouse: {},
      tArea: {},
      checkedKeys: [],
      checkedLevelNums: [],
      expandedKeys: [],
      areaTreeData: [],
      defaultCheckedKeys: [],
      areaSearch: {
        areaVisible: false,
        areaId: ''
      }
    }
  },
  methods: {
    showAreaSearch () {
      this.areaSearch.areaVisible = true
    },
    handleAreaSearchClose () {
      this.areaSearch.areaVisible = false
    },
    handleAreaSearchSuccess (area) {
      this.areaSearch.areaVisible = false
      console.log(area)
      this.form.setFieldsValue({
        areaName: area.text || area.id
      })
      this.areaSearch.areaId = area.id
    },
    reset () {
      // this.tHouse = {}
      // this.loading = false
      // this.expandedKeys = this.checkedKeys = this.defaultCheckedKeys = []
      // this.tArea = {}
      // this.form.resetFields()
    },
    onClose () {
      this.reset()
      this.$emit('close')
    },
    handleCheck (checkedKeys, info) {
      this.checkedKeys = checkedKeys
      info.checkedNodes.forEach(cn => {
        this.checkedLevelNums.push(cn.data.props.value)
      })
    },
    expandAll () {
      this.expandedKeys = this.allTreeKeys
    },
    closeAll () {
      this.expandedKeys = []
    },
    handleExpand (expandedKeys) {
      this.expandedKeys = expandedKeys
    },
    setFormValues ({ ...tHouse }) {
      console.log(tHouse)
      let { id, houseCode, houseAdr, areaId, areaName } = tHouse
      this.id = id
      this.checkedKeys.checked = [areaId]
      this.form.getFieldDecorator('houseCode')
      this.form.setFieldsValue({ houseCode: houseCode })
      this.form.getFieldDecorator('houseAdr')
      this.form.setFieldsValue({ houseAdr: houseAdr || '' })
      this.form.setFieldsValue({ areaName: areaName })
      // if (tHouse.parentId !== '0') {
      //   this.defaultCheckedKeys.push(tHouse.parentId)
      //   this.checkedKeys = this.defaultCheckedKeys
      //   this.expandedKeys = this.checkedKeys
      // }
    },
    handleSubmit () {
      // let checkedArr = Object.is(this.checkedKeys.checked, undefined) ? this.checkedKeys : this.checkedKeys.checked
      // if (checkedArr.length > 1) {
      //   this.$message.error('最多只能选择一个上级区域，请修改')
      //   return
      // }
      // if (checkedArr[0] === this.tHouse.shopId) {
      //   this.$message.error('不能选择自己作为上级区域，请修改')
      //   return
      // }
      this.form.validateFields((err, values) => {
        if (!err) {
          this.setHouseFields()
          this.loading = true
          this.tHouse.areaId = this.checkedKeys.checked[0]
          this.tHouse.id = this.id
          this.$put('tHouse', {
            ...this.tHouse
          })
            .then(() => {
              this.reset()
              this.$emit('success')
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    },
    setHouseFields () {
      let values = this.form.getFieldsValue(['houseCode', 'houseAdr', 'areaId'])
      if (typeof values !== 'undefined') {
        Object.keys(values).forEach(_key => {
          this.tHouse[_key] = values[_key]
        })
      }
    }
  },
  watch: {
    houseEditVisiable () {
      if (this.houseEditVisiable) {
        this.$get('tArea', {}).then(r => {
          this.areaTreeData = r.data.rows.children
          this.allTreeKeys = r.data.ids
        })
      }
    }
  }
}
</script>
